package com.bingxue.edu.management.education.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.education.model.entity.EnrollPermission;
import com.bingxue.edu.management.education.model.query.StudentQuery;
import com.bingxue.edu.management.education.model.req.EnrollPermissionReq;
import com.bingxue.edu.management.education.model.req.StudentCreateReq;
import com.bingxue.edu.management.education.model.req.StudentUpdateReq;
import com.bingxue.edu.management.education.model.resp.EnrollPermissionResp;
import com.bingxue.edu.management.education.model.resp.StudentDetailResp;
import com.bingxue.edu.management.education.model.resp.StudentResp;
import com.bingxue.edu.management.education.model.resp.StudentScheduleResp;
import com.bingxue.edu.management.education.model.resp.StudentStatResp;
import com.bingxue.edu.management.education.service.EnrollPermissionService;
import com.bingxue.edu.management.education.service.ParentService;
import com.bingxue.edu.management.education.service.StudentService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 学员接口
 *
 * @author panbo
 * @since 2024/07/25 00:02
 */
@Tag(name = "学员接口")
@RestController
@RequestMapping("/education/student")
@RequiredArgsConstructor
public class StudentController {

    private final StudentService studentService;
    private final EnrollPermissionService enrollPermissionService;
    private final ParentService parentService;

    @Operation(summary = "分页查询学生信息")
    @ApiResource("edu:student:manage")
    @GetMapping("/page")
    public Response<Pagination<StudentResp>> page(StudentQuery query, @Validated PageQuery pageQuery) {
        Page<StudentResp> page = studentService.getStudentPage(query, pageQuery);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询家长学生列表")
    @ApiResource("edu:student:manage")
    @GetMapping("/list-by-parent")
    public Response<List<StudentResp>> listByParent(@RequestParam String parentId) {
        return Response.success(studentService.getListByParentId(parentId));
    }

    @Operation(summary = "学生统计数量查询")
    @ApiResource("edu:student:manage")
    @GetMapping("/stat")
    public Response<StudentStatResp> statistic() {
        return Response.success(studentService.getStatData());
    }

    @Operation(summary = "查询学生详情")
    @ApiResource("edu:student:manage")
    @GetMapping("/detail/{id}")
    public Response<StudentDetailResp> detail(@PathVariable String id) {
        return Response.success(studentService.getDetailById(id));
    }

    @Operation(summary="创建学生信息")
    @ApiResource("edu:student:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated StudentCreateReq req) {
        studentService.create(req);
        return Response.success();
    }

    @Operation(summary = "更新学生信息")
    @ApiResource("edu:student:manage")
    @PostMapping("/update-student/{studentId}")
    public Response<Void> updateStudent(@RequestBody @Validated StudentUpdateReq req, @PathVariable String studentId) {
        studentService.update(req, studentId);
        return Response.success();
    }

    @Operation(summary = "删除学生信息")
    @ApiResource("edu:student:delete")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        studentService.removeStudentsByIds(ids);
        return Response.success();
    }

    @Operation(summary = "查询学生选课权限列表")
    @ApiResource("edu:student:manage")
    @GetMapping("/permission/list/{id}")
    public Response<List<EnrollPermissionResp>> getPermissionList(@PathVariable String id) {
        return Response.success(enrollPermissionService.getPermissionListByStudentId(id));
    }

    @Operation(summary = "创建学生选课权限")
    @ApiResource("edu:student:manage")
    @PostMapping("/permission/create")
    public Response<Void> createPermission(@RequestBody @Validated EnrollPermissionReq req) {
        enrollPermissionService.createPermission(req);
        return Response.success();
    }

    @Operation(summary = "更新学生选课权限")
    @ApiResource("edu:student:manage")
    @PostMapping("/permission/update/{id}")
    public Response<Void> updatePermission(@RequestBody @Validated EnrollPermissionReq req, @PathVariable String id) {
        EnrollPermission enrollPermission = enrollPermissionService.copyById(req,id);
        enrollPermissionService.updateById(enrollPermission);
        return Response.success();
    }

    @Operation(summary = "删除学生选课权限")
    @ApiResource("edu:student:delete")
    @PostMapping("/permission/delete/{id}")
    public Response<Void> deletePermission(@PathVariable String id) {
        enrollPermissionService.removeById(id);
        return Response.success();
    }

    @Operation(summary = "学生课表")
    @ApiResource("edu:student:manage")
    @GetMapping("/schedule/{id}")
    public Response<List<StudentScheduleResp>> getSchedule(@PathVariable String id) {
        return Response.success(studentService.getScheduleByStudentId(id));
    }

}
